<template>
  <div class="articles-container">
    <el-card class="articles-card">
      <!-- 表单区域 -->
      <el-form :model="searchData" ref="searchData">
        <el-row :gutter="10">
          <el-col :span="6"
            ><el-form-item label="关键字" prop="keyword">
              <el-input
                v-model="searchData.keyword"
                placeholder="根据文章标题搜索"
                style="width: 180px;"
                size="small"
                @keyup.enter="onSearch"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="状态" prop="state">
              <el-select
                v-model="searchData.state"
                placeholder="请选择"
                size="small"
                @keyup.enter="onSearch"
              >
                <el-option label="启用" value="0"></el-option>
                <el-option label="禁用" value="1"></el-option>
              </el-select>
              <el-button size="small" @click="onClear">清除</el-button>
              <el-button type="primary" size="small" @click="onSearch">
                搜索</el-button
              >
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <el-button
              type="success"
              icon="el-icon-edit"
              size="small"
              @click="handleChange('add')"
            >
              新增技巧</el-button
            >
          </el-col>
        </el-row>
      </el-form>
      <!-- /表单区域 -->

      <!-- 数据记录 -->
      <el-alert
        v-if="alertText !== ''"
        :title="alertText"
        type="info"
        show-icon
        :closable="false"
      ></el-alert>
      <!-- /数据记录 -->

      <!-- 表格区域 -->
<<<<<<< HEAD
      <el-table
        :data="articlesList"
        v-loading="listLoading"
        style="width: 100%"
      >
        <el-table-column type="index" label="序号" width="80px">
        </el-table-column>
        <el-table-column prop="title" label="文章标题" width="340px">
        </el-table-column>
        <el-table-column prop="visits" label="阅读数" width="120px">
        </el-table-column>
        <el-table-column prop="username" label="录入人" width="140px">
        </el-table-column>
        <el-table-column label="录入时间" width="180px">
=======
      <el-table :data="articlesList" v-loading="listLoading" style="width: 100%" fit>
        <el-table-column type="index" label="序号" width="80"> </el-table-column>
        <el-table-column prop="title" label="文章标题" width="400">
          <template slot-scope="scope">
            <span>{{ scope.row.title }}</span>
            <i class="el-icon-film" v-if="scope.row.videoURL" @click="playerVideo(scope.row)"></i>
          </template>
        </el-table-column>
        <el-table-column prop="visits" label="阅读数"> </el-table-column>
        <el-table-column prop="username" label="录入人"> </el-table-column>
        <el-table-column label="录入时间">
>>>>>>> 99b581ad90efc8af6413e4097d13ec8f4855db58
          <template slot-scope="scope">
            {{ scope.row.createTime | parseTimeByString }}
          </template>
        </el-table-column>
        <el-table-column label="状态" width="100px">
          <template slot-scope="scope">
            <span>{{ scope.row.state ? '已禁用' : '已启用' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click="handlePreview">预览</el-button>
            <el-button type="text" @click="onDisabled(scope.row)"
              >{{ scope.row.state ? '启用' : '禁用' }}
            </el-button>
            <el-button
              type="text"
              :disabled="!scope.row.state"
              @click="handleChange(scope.row.id)"
              >修改</el-button
            >
            <el-button
              type="text"
              :disabled="!scope.row.state"
              @click="removeUser(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- /表格区域 -->

      <!-- 分页区域 -->
      <div class="pagination">
        <div class="pages">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="Number(queryInfo.page)"
            :total="Number(total)"
            :page-size="Number(queryInfo.pagesize)"
            :page-sizes="[5, 10, 20, 30]"
            layout="prev, pager, next, sizes, jumper"
          ></el-pagination>
        </div>
      </div>
      <!-- /分页区域 -->

      <!-- 新增技巧对话框 -->
<<<<<<< HEAD
      <articles-add
        ref="editArticle"
        :titleInfo="titleInfo"
        :articlesAddInfo="formData"
        v-on:articleNewDataes="getArticlesList"
      ></articles-add>
      <!-- /新增技巧对话框 -->

      <!-- 预览对话框 -->
      <articles-preview
        ref="previewArticle"
        :articleInfo="articleInfo"
        v-on:newDataes="getArticlesList"
      >
      </articles-preview>
=======
      <articles-add ref="editArticle" :titleInfo="titleInfo" :articlesAddForm="articlesAddData" v-on:articlesAddDatas="getArticlesList"></articles-add>
      <!-- /新增技巧对话框 -->

      <!-- 预览对话框 -->
      <articles-preview ref="previewArticle" :previewForm="previewData" v-on:previewDatas="getArticlesList"> </articles-preview>
>>>>>>> 99b581ad90efc8af6413e4097d13ec8f4855db58
      <!-- /预览对话框 -->
    </el-card>
  </div>
</template>

<script>
// 父组件调用的接口
import { list, changeState, detail, remove, simple } from '@/api/hmmm/articles'
// 时间格式
import index from '@/filters/index'
// 导入新增技巧子组件
import ArticlesAdd from '../components/articles-add'
// 导入预览子组件
import ArticlesPreview from '../components/articles-preview'

export default {
  name: 'ArticlesList',
  // 子组件
  components: {
    ArticlesAdd,
    ArticlesPreview
  },
  data() {
    return {
      // 文章列表数据
      articlesList: [],
      // listLoading 加载
      listLoading: true,
      // 数据记录
      alertText: '',
      // 页数
      queryInfo: {
        // 当前页数
        page: 1,
        // 每页显示信息条数
        pagesize: 10
      },
      // 页总数
      total: null,
      searchData: {
        keyword: '',
        state: ''
      },
      titleInfo: {
        pageTitle: '文章', // 页面标题
        text: '' // 新增、编辑文本
      },
      articlesAddData: {
        title: '',
        articleBody: '',
        videoURL: '',
        status: false
      },
      previewData: {
        title: '',
        articleBody: '',
        createTime: '',
        username: '',
        visits: ''
      }
    }
  },

  methods: {
    // 获取文章列表数据
    async getArticlesList(params) {
      this.listLoading = true
      const { data: res } = await list(params)
      // console.log(res)
      // 获取数据给ArticlesList
      this.articlesList = res.items
      // 获取总条数
      this.total = res.counts
      this.alertText = `数据一共 ${this.total} 条`
      this.listLoading = false
    },

    // 重置
    onClear() {
      // console.log('onClear')
      this.$refs.searchData.resetFields()
    },

    // 搜索
    onSearch() {
      console.log(this.searchData)
      this.getArticlesList(this.searchData)
    },

    // 操作启用，禁用
    async onDisabled(id) {
      // console.log(id)
      const sta = id.state
      const data = await changeState({
        id: id.id,
        state: sta === 1 ? 0 : 1
      })

      if (data.status !== 200) {
        return this.$message.error('操作失败！')
      }
      this.$message.success('操作成功！')
      // console.log(data)
      id.state = !id.state
<<<<<<< HEAD
      // this.getArticlesList(this.queryInfo)
      // 如果是已启用状态，操作栏中的按钮：禁用按钮，修改和删除按钮需要不可用。
      // 如果是已禁用状态，操作栏中的按钮：禁用按钮，修改和删除按钮需要可用。
      //  更新视图
=======
      // this.getArticlesList()
>>>>>>> 99b581ad90efc8af6413e4097d13ec8f4855db58
    },

    // 监听 pagesize 改变的事件
    handleSizeChange(newSize) {
      // console.log(`每页 ${val} 条`)
      this.queryInfo.pagesize = newSize
      this.getArticlesList(this.queryInfo)
    },

    // 监听 page 改变的事件
    handleCurrentChange(newPage) {
      // console.log(`当前页: ${val}`)
      this.queryInfo.page = newPage
      this.getArticlesList(this.queryInfo)
    },

    // 添加、编辑之后表单清空
    query() {
      this.articlesAddData = {}
    },

    // 新增文章、修改文章
    handleChange(id) {
      this.query()
      this.$refs.editArticle.dialogShow()
      if (id === 'add') {
        this.titleInfo.text = '新增' // 给弹层传参
      } else {
        this.titleInfo.text = '修改' // 给弹层传参
        this.hanldeEditForm(id)
      }
    },

    // 预览
    async handlePreview(id) {
      this.$refs.previewArticle.articleShow()
      // console.log(id)
    },

    // 编辑
    // 表单详情数据加载
<<<<<<< HEAD
    async hanldeEditForm(id) {
      this.formData.id = id
      const { data: res } = await detail({ id: id })
      // 获取详情 给formData
      this.formData = res
    },

=======
    async hanldeEditForm(idd) {
      this.articlesAddData.id = idd
      const { data: res } = await detail({ id: idd })
      // console.log(res)
      // 获取详情 articlesAddData
      this.articlesAddData = res
    },

    // 预览
    handlePreview(item) {
      this.$refs.previewArticle.dialogShow()
      this.previewData = item
      // console.log(item)
    },

    // 视频
    closeChange() {
      this.videoShow = false
    },

    async playerVideo(item) {
      this.videoShow = true
      if (item.videoURL !== null) {
        this.videourl = item.videoURL
      }
    },

>>>>>>> 99b581ad90efc8af6413e4097d13ec8f4855db58
    // 删除
    removeUser(id) {
      this.$confirm('此操作将永久删除用户 ' + ', 是否继续?', '提示', {
        type: 'warning'
      })
        .then(async () => {
          await remove({ id: id })
            .then(response => {
              this.$message.success('成功删除了用户' + '!')
              this.dataList.splice(user, 1)
              this.getList(this.requestParameters)
            })
            .catch(response => {
              this.$message.error('删除失败!')
            })
        })
        .catch(() => {
          this.$message.info('已取消操作!')
        })
    },

    // 挂载结束
    mounted: function() {},
    // 创建完毕状态
    created() {
      var that = this
      this.getArticlesList()
      // 键盘enter操作
      document.onkeydown = function(e) {
        var key = window.event.keyCode
        if (key === 13) {
          that.onSearch(this.searchData)
        }
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" scoped lang="scss">
.articles-container {
  .el-card {
    margin: 10px;
    .el-form {
      padding: 0 15px 0 20px;
      .el-select {
        padding-right: 10px;
      }
    }
    .el-table {
      margin-top: 15px;
    }
    .pagination {
      margin-top: 10px;
      display: flex;
      justify-content: flex-end;
    }
  }
}
</style>
